﻿package mortal.game.view.achievement.data
{
    import Message.DB.Tables.*;
    import com.gengine.resource.*;
    import mortal.game.view.common.*;

    public class AchievementConfig extends Object
    {
        public var categoryObj:Object;
        public var categoryArr:Array;
        public var categoryRewardObj:Object;
        public var categoryRewardMaxLevelObj:Object;
        public var typeObj:Object;
        public var achievementObj:Object;
        private var _sumPoint:uint = 0;
        private static var _instance:AchievementConfig;

        public function AchievementConfig()
        {
            this.categoryObj = {};
            this.categoryArr = [];
            this.categoryRewardObj = {};
            this.categoryRewardMaxLevelObj = {};
            this.typeObj = {};
            this.achievementObj = {};
            this.init();
            return;
        }// end function

        private function init() : void
        {
            var _loc_1:* = ConfigManager.instance.getJSONByFileName("t_achievement_category.json");
            this.wirteCategory(_loc_1);
            _loc_1 = ConfigManager.instance.getJSONByFileName("t_achievement_category_rewards.json");
            this.wirteCategoryReward(_loc_1);
            _loc_1 = ConfigManager.instance.getJSONByFileName("t_achievement_type.json");
            this.wirteType(_loc_1);
            _loc_1 = ConfigManager.instance.getJSONByFileName("t_achievement.json");
            this.wirteAchievement(_loc_1);
            return;
        }// end function

        private function wirteCategory(param1:Object) : void
        {
            var _loc_2:Object = null;
            var _loc_3:TAchievementCategory = null;
            for each (_loc_2 in param1)
            {
                
                _loc_3 = new TAchievementCategory();
                ClassTypesUtil.copyValue(_loc_3, _loc_2);
                this.categoryObj[_loc_3.id] = _loc_3;
                this.categoryArr.push(_loc_3);
            }
            if (this.categoryArr.length > 0)
            {
                this.categoryArr.sortOn(["id"], Array.NUMERIC);
            }
            return;
        }// end function

        private function wirteCategoryReward(param1:Object) : void
        {
            var _loc_3:Object = null;
            var _loc_4:TAchievementCategoryRewards = null;
            var _loc_2:int = 0;
            for each (_loc_3 in param1)
            {
                
                _loc_4 = new TAchievementCategoryRewards();
                ClassTypesUtil.copyValue(_loc_4, _loc_3);
                this.categoryRewardObj[_loc_4.catagoryId + "_" + _loc_4.level] = _loc_4;
                _loc_2 = int(this.categoryRewardMaxLevelObj["" + _loc_4.catagoryId]);
                _loc_2 = Math.max(_loc_2, _loc_4.level);
                this.categoryRewardMaxLevelObj["" + _loc_4.catagoryId] = _loc_2;
            }
            return;
        }// end function

        private function wirteType(param1:Object) : void
        {
            var _loc_2:Object = null;
            var _loc_3:TAchievementType = null;
            for each (_loc_2 in param1)
            {
                
                _loc_3 = new TAchievementType();
                ClassTypesUtil.copyValue(_loc_3, _loc_2);
                this.typeObj[_loc_2.id] = _loc_3;
            }
            return;
        }// end function

        private function wirteAchievement(param1:Object) : void
        {
            var _loc_2:Object = null;
            var _loc_3:TAchievement = null;
            for each (_loc_2 in param1)
            {
                
                _loc_3 = new TAchievement();
                ClassTypesUtil.copyValue(_loc_3, _loc_2);
                this.achievementObj[_loc_2.id] = _loc_3;
            }
            return;
        }// end function

        public function getTotalPoint() : uint
        {
            var _loc_1:TAchievement = null;
            if (this._sumPoint != 0)
            {
                return this._sumPoint;
            }
            for each (_loc_1 in this.achievementObj)
            {
                
                if (_loc_1)
                {
                    this._sumPoint = this._sumPoint + _loc_1.achievementPoint;
                }
            }
            return this._sumPoint;
        }// end function

        public static function get instance() : AchievementConfig
        {
            if (_instance == null)
            {
                _instance = new AchievementConfig;
            }
            return _instance;
        }// end function

    }
}
